<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="550px"
            @close="handleClose"
            @confirm="handleSubmit"
        >
            <el-form ref="formRef" :model="formData" label-width="90px">
                <el-form-item label="图片" prop="image">
                    <upload
                        v-model="files"
                        :limit="1"
                        :max-size="2"
                        btn-text="上传图片"
                        list-type="picture-card"
                        showProgress
                    >
                    </upload>
                </el-form-item>
                <el-form-item label="选择代理商" prop="agent_id">
                    <el-select v-model="formData.agent_id" clearable placeholder="请选择代理商">
                        <el-option v-for="item in agent_list" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="formData.sort" clearable placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="分类" prop="type">
                    <el-select v-model="formData.type" placeholder="请选择分类">
                        <el-option label="主页轮播图" value="index_banner" />
                        <el-option label="下单页轮播图" value="order_banner" />
                        <el-option label="用户焦点图" value="user_focus" />
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" name="boomBannerEdit" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomBannerAdd, apiBoomBannerDetail, apiBoomBannerEdit } from '@/api/boom_banner'
import type { PropType } from 'vue'
import { apiBoomAgentLists } from '@/api/agent'
import Upload from '@/components/my-upload/index.vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const agent_list = ref([])
const files = ref<any>([])

const getGentLists = async () => {
    const data = await apiBoomAgentLists({
        page: 1,
        page_size: 10000
    })
    agent_list.value = data.lists
}

getGentLists()


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑轮播图管理' : '新增轮播图管理'
})

// 表单数据
const formData = reactive({
    id: '',
    image: '',
    agent_id: '',
    sort: '',
    type: ''
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    files.value = [
        {
            url: data.image
        }]

}


const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomBannerDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, image: files.value[0].url }
    mode.value == 'edit'
        ? await apiBoomBannerEdit(data)
        : await apiBoomBannerAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = async (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
